package com.yueyang.sojourn.server.service.sojourn.project;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yueyang.sojourn.server.common.baymax.web.result.JAssert;
import com.yueyang.sojourn.server.model.enums.OperStatusEnum;
import com.yueyang.sojourn.server.model.po.SojournProject;
import com.yueyang.sojourn.server.model.vo.SojournProjectVo;
import com.yueyang.sojourn.server.service.attachdetail.AttachDetailService;
import com.yueyang.sojourn.server.service.sojourn.geolocation.SojournGeoLocationService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yemingxing
 * @summary 项目信息 只读service
 * @Copyright (c) 2020, Yueyang Group All Rights Reserved.
 * @since 2020-02-15
 */
@Service
public class ReadProjectService {

    @Autowired
    private SojournProjectService sojournProjectService;

    @Autowired
    private AttachDetailService attachDetailService;

    @Autowired
    private SojournGeoLocationService sojournGeoLocationService;

    /**
     * 分页查询
     *
     * @param loginUserId 登录ucid
     * @param keyWord     关键字
     * @param operStatus  营业状态{@link OperStatusEnum#values()}
     * @param pageNo      页码
     * @param pageSize    页大小
     * @return PageInfo<SojournProjectVo>
     */
    public PageInfo<SojournProjectVo> findPage(long loginUserId, String keyWord, Integer operStatus, int pageNo, int pageSize) {
        // todo-ymx 关键词搜索 like
        PageInfo<SojournProject> pageInfo = PageHelper.startPage(pageNo, pageSize)
                .doSelectPageInfo(() -> sojournProjectService.findByOperStatus(keyWord, Optional.ofNullable(operStatus).map(Collections::singletonList).orElse(null)));

        PageInfo<SojournProjectVo> voPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo, voPageInfo);
        voPageInfo.setList(pageInfo.getList().parallelStream()
                .map(sojournProject -> po2vo(sojournProject, ProjectVoType.GEO_LOCATION, ProjectVoType.HOME_PIC))
                .collect(Collectors.toList()));

        return voPageInfo;
    }

    private SojournProjectVo po2vo(SojournProject sojournProject, ProjectVoType... voType) {
        SojournProjectVo sojournProjectVo = new SojournProjectVo();
        if (Objects.isNull(sojournProject)) {
            return null;
        }
        BeanUtils.copyProperties(sojournProject, sojournProjectVo);
        List<ProjectVoType> voTypes = Arrays.asList(voType);
        if (CollectionUtils.isEmpty(voTypes) || voTypes.contains(ProjectVoType.GEO_LOCATION)) {
            sojournProjectVo.setGeoLocation(sojournGeoLocationService.selectByProjectId(sojournProject.getId()));
        }
        if (CollectionUtils.isEmpty(voTypes) || voTypes.contains(ProjectVoType.HOME_PIC)) {
            sojournProjectVo.setHomePic(attachDetailService.selectByPrimaryKey(sojournProject.getHomeAttachId()));
        }
        if (CollectionUtils.isEmpty(voTypes) || voTypes.contains(ProjectVoType.OTHERS_PIC)) {
            sojournProjectVo.setOthersPic(attachDetailService.selectListByProjectId(sojournProject.getId()));
        }
        sojournProjectVo.setBasicFacility(JSON.parseArray(sojournProject.getBasicFacility(), Integer.class));
        sojournProjectVo.setCatering(JSON.parseArray(sojournProject.getCatering(), Integer.class));
        sojournProjectVo.setRecreation(JSON.parseArray(sojournProject.getRecreation(), Integer.class));
        return sojournProjectVo;
    }

    public List<SojournProjectVo> findVoByLayoutId(int layoutId, Integer operStatus) {
        List<SojournProject> projectList = sojournProjectService.selectByLayoutId(layoutId, operStatus);
        return projectList.parallelStream().map(po -> this.po2vo(po, ProjectVoType.GEO_LOCATION, ProjectVoType.HOME_PIC)).collect(Collectors.toList());
    }

    public enum ProjectVoType {
        /**
         * vo字段取值规则
         */
        GEO_LOCATION, HOME_PIC, OTHERS_PIC
    }

    public SojournProjectVo selectVoByPrimaryKey(Long id, ProjectVoType... voType) {
        SojournProject sojournProject = sojournProjectService.selectByPrimaryKey(id);
        if (Objects.isNull(sojournProject)) {
            JAssert.justFailed("不存在的项目id：" + id);
        }
        return po2vo(sojournProject, voType);
    }

    /**
     * @param operStatusStr 营业状态{@link OperStatusEnum#values()}, 多个逗号间隔
     * @param pageNo        页码
     * @param pageSize      页大小
     * @return PageInfo<SojournProjectVo>
     */
    public PageInfo<SojournProjectVo> selectByOperStatus(String operStatusStr, int pageNo, int pageSize) {
        List<Integer> operStatus = Arrays.stream(operStatusStr.split(",")).map(Integer::valueOf).collect(Collectors.toList());
        PageInfo<SojournProject> pageInfo = PageHelper.startPage(pageNo, pageSize)
                .doSelectPageInfo(() -> sojournProjectService.findByOperStatus(null, operStatus));

        PageInfo<SojournProjectVo> voPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo, voPageInfo);
        voPageInfo.setList(pageInfo.getList().parallelStream()
                .map(sojournProject -> {
                    SojournProjectVo sojournProjectVo = new SojournProjectVo();
                    BeanUtils.copyProperties(sojournProject, sojournProjectVo);
                    return sojournProjectVo;
                })
                .collect(Collectors.toList()));

        return voPageInfo;
    }
}
